package com.soft.line.service.impl;

import com.soft.line.entity.CodeDetail;
import com.soft.line.entity.LasCodeItem;
import com.soft.line.entity.LasDepartmentInfo;
import com.soft.line.filter.HttpRequestUtil;
import com.soft.line.filter.UserTokenDto;
import com.soft.line.mapper.CodeDetailMapper;
import com.soft.line.mapper.LasCodeItemMapper;
import com.soft.line.mapper.LasDepartmentInfoMapper;
import com.soft.line.mapper.RoleInfoMapper;
import com.soft.line.service.CodeDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class CodeDetailServiceImpl implements CodeDetailService {
    @Autowired
    private CodeDetailMapper codeDetailMapper;
    @Autowired
    private LasCodeItemMapper lasCodeItemMapper;
    @Autowired
    private RoleInfoMapper roleInfoMapper;
    @Autowired
    private LasDepartmentInfoMapper lasDepartmentInfoMapper;

    @Override
    public LasCodeItem selectByPrimaryKey(String itemTypeId, String itemVal) {
        return lasCodeItemMapper.selectByPrimaryKey(itemTypeId,itemVal);
    }

    @Override
    public LasCodeItem selectByItemDesc(String itemTypeId, String itemDesc) {
        return lasCodeItemMapper.selectByItemDesc(itemTypeId,itemDesc);
    }

    @Override
    public List<LasCodeItem> selectByCodeType(String itemTypeId) {
        return  lasCodeItemMapper.selectByCodeType(itemTypeId);
    }

    /*@Override
    public List<CodeDetail> getDepByRole() {
        List<CodeDetail> depList = new ArrayList<CodeDetail>();
        UserTokenDto userToken= HttpRequestUtil.getUserToken();
        if(userToken!=null){
            String roleName = roleInfoMapper.selectByPrimaryKey(userToken.getUserInfo().getRoleId()).getName();
            if (roleName.contains("管理员")){
                depList = codeDetailMapper.selectByCodeType("1");
            }else {
                depList = codeDetailMapper.getDepByRole(userToken.getUserInfo().getId());
            }
        }
        return depList;
    }*/

    @Override
    public List<LasDepartmentInfo> getDepByRole() {
        List<LasDepartmentInfo> depList = new ArrayList<LasDepartmentInfo>();
        UserTokenDto userToken= HttpRequestUtil.getUserToken();
        if(userToken!=null){
            String roleName = userToken.getUserInfo().getRoleName();
            if (roleName.contains("管理员")||roleName.contains("归档登记员")){
                depList = lasDepartmentInfoMapper.selectAllSuperior();
            }else {
                LasDepartmentInfo departmentInfo = lasDepartmentInfoMapper.getDepByRole(userToken.getUserInfo().getId());
                departmentInfo = getSupDep(departmentInfo.getId());
                depList.add(departmentInfo);
            }
        }
        return depList;
    }

    @Override
    public List<LasDepartmentInfo> selectAllDep() {
        return lasDepartmentInfoMapper.selectAllSuperior();
    }

    @Override
    public LasDepartmentInfo selectSuperiorDepByDepName(String depName) {
        return lasDepartmentInfoMapper.selectSuperiorDepByDepName(depName);
    }

    public LasDepartmentInfo getSupDep(String depId) {
        LasDepartmentInfo lasDepartmentInfo = lasDepartmentInfoMapper.selectSupDepByDepId(depId);
        while (lasDepartmentInfo!=null && !lasDepartmentInfo.getSuperiorDepId().equals("0")){
            lasDepartmentInfo = getSupDep(lasDepartmentInfo.getId());
        }
        lasDepartmentInfo = lasDepartmentInfoMapper.selectSuperiorDepByDepId(depId);
        return lasDepartmentInfo;
    }

}
